// pages/my_content/user_login/user_login.js
const app = getApp(); 
Page({

  /**
   * 页面的初始数据
   */
  data: {
    login_create_status : false, //是否可以登录的转化控制

    show_back : false,
    show: false,
    buttons: [ { type: 'default', text: '拒绝', value: 0 }, { type: 'primary', text: '允许', value: 1 } ]
  },

  vires_control(e){
    let that = this
    that.setData({show : true})
  },

  buttontap(e){
    let that = this
    //console.log(e.detail.item)
    if(e.detail.item.type === "default")  that.setData({show : false})
    else{
      that.setData({show : false})

      that.login_wechat()
    }
  },

  login_wechat(){ // 微信登录按钮的点击事件
    const that = this
    const db = wx.cloud.database()
    const todo = db.collection('log-in')
    todo.where({ wechat : app.globalData.user_openID }).get({
      success: function (res) {
        if (res.data.length >= 1){
          that.login_info = res.data[0] //暂存用户登录信息
          that.create_user_info() //初始化用户信息表
        }
        else {
          todo.add({
            data: {
              wechat : app.globalData.user_openID,
              account : "WeChat",
              password : "",
              mode : true,
            },
            success: function(res) {
              that.login_info = res.data //暂存用户登录信息
              that.create_user_info() //初始化用户信息表
            }
          })
        }
        that.login_status = true //暂存登陆状态
      },
    })
  },



  checkboxChange(e) {   //控制用户是否可以登录
    let that = this
    //console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    if(e.detail.value.length == 1 && e.detail.value[0] == "agree"){
      that.setData({ login_create_status : true})
    }else{
      that.setData({ login_create_status : false})
    }
  },

  create_user_info(){ //初始化用户信息表
    let that = this
    const db = wx.cloud.database()
    const todo = db.collection('user')
    todo.where({ _id : that.login_info._id }).get({
      success: function (res) {
        if (res.data.length >= 1){
          that.user_info = res.data[0]
          wx.navigateBack(); 
        }else{
          todo.add({
            data: {
              _id : that.login_info._id,
              avatar : "https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0",
              nickname : "",
              gender : "",
              age : 0,
              district : {city : "", province : "", region : ""},
            },
            success: function(res) {
              that.create_user_info()
            }
          })
        }
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    let that = this

    that.login_info = {}
    that.login_status = false
    that.user_info = {}
  },


  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    let that = this
   
    wx.setStorage({ key : "login_info", data : that.login_info }) //缓存用户登录信息
    wx.setStorage({ key : "login_status", data : that.login_status })  //缓存用户登录状态
    wx.setStorage({ key : "user_info", data : that.user_info }) //缓存用户个人信息
    
  },


})